package subnarines;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public abstract class SeaObject {
    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;

    public static final int LIVE = 0;
    public static final int DEAD = 1;
    protected int state = LIVE;

    public SeaObject(int width,int height){  //潜艇
        this.height = height;
        this.width = width;
        x = -width;
        Random ran = new Random();
        y = ran.nextInt(World.HEIGHT-height-150+1)+150;
        speed = ran.nextInt(3)+1;
    }
    public SeaObject(int width,int height,int x,int y,int speed){  //船，水雷，炸弹
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public abstract void move();  //移动

    public abstract ImageIcon getImage();  //获取图片

    public boolean isOutOfBounds(){  //判断越界
        return this.x > World.WIDTH;
    }

    public boolean isHit(SeaObject other){  //碰撞
        int x1 = this.x-other.width;
        int x2 = this.x+this.width;
        int y1 = this.y-other.height;
        int y2 = this.y+this.height;
        int x = other.x;
        int y = other.y;
        return x>=x1 && x<=x2 && y>=y1 && y<=y2;
    }

    public void goDead(){  //寄
        state = DEAD;
    }

    public boolean isLive(){
        return state == LIVE;
    }
    public boolean isDead(){
        return state == DEAD;
    }

    public void paintImage(Graphics g){
        if(isLive()){
            this.getImage().paintIcon(null,g,this.x,this.y);
        }
    }


}
